<!--
 * Copyright 2022 The kubegems.io Authors
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
-->

<template>
  <v-flex>
    <v-form ref="form" v-model="valid" lazy-validation @submit.prevent>
      <BaseSubTitle :title="$t('tip.runtime')" />
      <v-card-text class="pa-2">
        <v-sheet v-for="(image, index) in runningImages" :key="index" class="grey lighten-4 rounded mb-3">
          <v-list-item two-line>
            <v-list-item-content class="py-2">
              <v-list-item-subtitle class="text-subtitle-2 py-1 primary--text" />
              <v-list-item-subtitle class="text-body-2 py-0">
                <v-list-item class="float-left pa-0" two-line>
                  <v-list-item-content class="py-0">
                    <v-list-item-title class="text-subtitle-2 py-1">
                      {{ image }}
                    </v-list-item-title>
                    <v-list-item-subtitle class="text-body-2 py-1"> {{ $t('tip.image') }} </v-list-item-subtitle>
                  </v-list-item-content>
                </v-list-item>
                <v-list-item v-if="index === 0" class="float-right pa-0" two-line>
                  <v-list-item-content class="py-0">
                    <v-list-item-title class="text-subtitle-2 py-1">
                      {{ runtime.istioVersion ? runtime.istioVersion : '' }}&nbsp;
                    </v-list-item-title>
                    <v-list-item-subtitle class="text-body-2 py-1"> {{ $t('tip.version') }} </v-list-item-subtitle>
                  </v-list-item-content>
                </v-list-item>
              </v-list-item-subtitle>
              <div class="kubegems__clear-float" />
            </v-list-item-content>
          </v-list-item>
        </v-sheet>
        <v-sheet v-if="runningImages.length === 0" class="grey lighten-4 rounded mb-2 text-center py-5 text-subtitle-1">
          {{ $root.$t('data.no_data') }}
        </v-sheet>
      </v-card-text>
      <BaseSubTitle :title="$t('tip.publish_soon')" />
      <v-card-text class="pa-2">
        <v-row>
          <v-col v-for="(image, index) in publishImages" :key="index" class="py-4" cols="12">
            <v-sheet class="float-left" :min-width="650" :width="650">
              <v-text-field
                class="my-0"
                dense
                full-width
                :label="$t('tip.image')"
                readonly
                required
                :rules="baseRules.publishRuler[image]"
                :value="image"
              />
            </v-sheet>
            <v-sheet class="float-right" :min-width="300" :width="300">
              <v-autocomplete
                v-model="base.images[image].tag"
                class="my-0 py-0"
                color="primary"
                dense
                flat
                full-width
                hide-details
                :items="tags[image]"
                :no-data-text="$root.$t('data.no_data')"
                :search-input.sync="base.images[image].tagtext"
                solo
                @change="onTagChange(image)"
                @keyup.enter="createImageTag(image)"
              >
                <template #selection="{ item, disabled }">
                  <v-chip class="ma-1" color="primary" :disabled="disabled" small>
                    {{ item['text'] }}
                  </v-chip>
                </template>
              </v-autocomplete>
            </v-sheet>
            <div class="kubegems__clear-float" />
          </v-col>

          <v-col cols="6">
            <v-text-field
              v-model="base.istioVersion"
              class="my-0"
              :label="$t('tip.tip.version')"
              required
              :rules="baseRules.versionRules"
            >
              <template #append>
                <v-btn color="primary" small text @click="generateVersion">
                  {{ $t('tip.auto_generate_version') }}
                </v-btn>
              </template>
            </v-text-field>
          </v-col>
        </v-row>
      </v-card-text>
    </v-form>
  </v-flex>
</template>

<script>
  import { getAppImageTags } from '@kubegems/api/direct';
  import { required } from '@kubegems/extension/ruler';
  import { mapGetters, mapState } from 'vuex';

  import messages from '../../../i18n';

  export default {
    name: 'BaseDeployInfoForm',
    i18n: {
      messages: messages,
    },
    props: {
      runtime: {
        type: Object,
        default: () => null,
      },
    },
    data() {
      return {
        valid: false,
        runningImages: [],
        publishImages: [],
        tags: {},
        base: {
          images: {},
          istioVersion: '',
        },
      };
    },
    computed: {
      ...mapState(['Circular']),
      ...mapGetters(['Tenant', 'Project']),
      baseRules() {
        const ruler = {
          versionRules: [required],
        };
        const publishRuler = {};
        this.publishImages.forEach((img) => {
          publishRuler[img] = [
            (v) => !!(this.splitImage(this.base.images[v].publish, 'tag') !== '') || this.$root.$t('ruler.required'),
          ];
        });
        ruler['publishRuler'] = publishRuler;
        return ruler;
      },
    },
    watch: {
      runtime: {
        handler: function () {
          this.generateBaseData();
        },
        immediate: true,
        deep: true,
      },
    },
    methods: {
      async appImageTags(image) {
        const data = await getAppImageTags(this.Tenant().ID, this.Project().ID, {
          image: image,
        });
        const tags = data.map((d) => {
          return {
            text: d.unpublishable ? `${d.name}(${this.$t('tip.unpublished')})` : d.name,
            value: d.name,
            disabled: d.unpublishable,
            ...d,
          };
        });
        this.$set(this.tags, image, tags);
      },
      generateBaseData() {
        this.publishImages = [];
        this.runningImages = [];
        if (this.runtime?.images) {
          Object.keys(this.runtime?.images).forEach((img) => {
            const image = this.runtime?.images[img];
            const tag = this.splitImage(image.publish, 'tag');
            if (image.running && image.running.length > 0) {
              this.runningImages.push(image.running);
            }
            this.publishImages.push(img);
            this.appImageTags(img);

            this.base.images[img] = {
              tag: tag,
              tagtext: '',
              publish: image.publish,
              running: image.running,
            };
          });
        }
      },
      createImageTag(image) {
        const tags = this.tags[image];
        if (
          !tags.find((tag) => {
            return tag.value === this.base.images[image].tagtext;
          })
        ) {
          tags.push({
            text: this.base.images[image].tagtext,
            value: this.base.images[image].tagtext,
            disabled: false,
            image: `${image}:${this.base.images[image].tagtext}`,
            name: this.base.images[image].tagtext,
          });
        }
        this.$set(this.tags, image, tags);
        this.$set(this.base.images[image], 'tag', this.base.images[image].tagtext);
        this.$set(this.base.images[image], 'tagtext', '');
        this.base.images[image].publish = `${image}:${this.base.images[image].tag}`;
      },
      onTagChange(image) {
        this.base.images[image].publish = `${image}:${this.base.images[image].tag}`;
      },
      splitImage(image, type) {
        const index = image.lastIndexOf(':');
        if (type === 'image') {
          return image.substr(0, index);
        } else if (type === 'tag') {
          return image.substr(index + 1);
        }
        return '';
      },
      generateVersion() {
        if (this.runtime.istioVersion) {
          const lastversion = this.runtime.istioVersion[this.runtime.istioVersion.length - 1];
          if (!isNaN(lastversion)) {
            this.base.istioVersion = `${this.runtime.istioVersion.substr(0, this.runtime.istioVersion.length - 1)}${
              parseInt(lastversion) + 1
            }`;
          } else {
            this.base.istioVersion = `${this.runtime.istioVersion}-v1`;
          }
        }
      },
      reset() {
        this.runningImages = [];
        this.publishImages = [];
        this.tags = {};
        this.base = this.$options.data().base;
        this.$refs.form.resetValidation();
      },
      validate() {
        return this.$refs.form.validate(true);
      },
      getData() {
        return this.base;
      },
    },
  };
</script>
